﻿namespace Manager
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    using Util;

    /// <summary>
    /// SubjectManager 的摘要说明
    /// </summary>
    public class SubjectManager
    {
        public SubjectManager()
        {
        }

        /// <summary>
        /// 获得指定ID的主题对象，不含其内的Theme User
        /// </summary>
        /// <param name="sid">主题ID</param>
        /// <returns>主题对象</returns>
        public Subject GetSubject(int sid)
        {
            try
            {
                WishWallDataContext dataContext = new WishWallDataContext();

                Subject subject = (from s in dataContext.Subject where s.sid == sid select s).Single<Subject>();

                return subject;
            }
            catch (Exception e)
            {
                LogUtil.InfoException(e);
                return null;
            }
        }


        /// <summary>
        /// 获得指定ID的主题对象，含其内的Theme User
        /// </summary>
        /// <param name="sid">主题ID</param>
        /// <returns>主题对象</returns>
        public Subject GetFullSubject(int sid)
        {
            WishWallDataContext dataContext = new WishWallDataContext();

            Subject subject = (from s in dataContext.Subject
                               //from u in s.Users
                               //from t in s.Theme
                               where s.sid == sid
                               select s
                              ).Single<Subject>();
            
            return subject;
        }


        /// <summary>
        /// 验证权限为需要密码时的查看权限
        /// </summary>
        /// <param name="sid">主题ID</param>
        /// <param name="viewCode">查看密码</param>
        /// <returns></returns>
        public bool ValidateViewLimit(int sid, string viewCode)
        {
            WishWallDataContext dataContext = new WishWallDataContext();
            int count = (from s in dataContext.Subject where s.sid == sid && s.viewCode==viewCode select s).Count();

            if (count > 0)
                return true;
            else
                return false;
        }
    }
}